#include "stdafx.h"
#include "chdir.h"
#ifdef WIN32
#include <direct.h>
#else
#define _chdir chdir
#define _getcwd getcwd
#include <unistd.h>
#endif

#ifndef  _MAX_PATH
const int _MAX_PATH = 400; /* max. length of full pathname */
#endif

#include <string.h>
#include <ctype.h>


char* get_working_directory(char* path)
{
    if (_getcwd(path, _MAX_PATH) == NULL)
        perror("_getcwd error");
    else {
        return (path);
    }
    return(0);
}


int Print_Working_Directory(void)
{
    char path[_MAX_PATH];

    if (get_working_directory(path)) {
        cout << "The working directory is " << path << endl;
    }
    return(0);
}

int toparentdir(void)
{
    int i, l;
    char path[_MAX_PATH];

    if (_getcwd(path, _MAX_PATH) == NULL)
        perror("_getcwd error");

    l = (int)strlen(path);
    for (i = l - 1; i >= 0; i--) {
#ifdef WIN32
        if (path[i] == '\\')
#else
        if (path[i] == '/')
#endif
            break;
        else if (path[i] == ':') {
            printf("Already in the root");
            return(-1);
        }
    }
    path[i] = '\0';

    if (_chdir(path)) {
        printf("Unable to locate the directory: %s\n", path);
        return(-1);
    } else if (path[i-1] == ':') {
        printf("Disk root");
        return(0);// in the root
    } else {
        return(1);
    }
}

int tochilddir(const char* dirname)
{
    int l;
    char path[_MAX_PATH];

    if (_getcwd(path, _MAX_PATH) == NULL)
        perror("_getcwd error");

    l = (int)strlen(path);
#ifdef WIN32
    path[l] = '\\';
#else
    path[l] = '/';
#endif
    strcpy(&path[l+1], dirname);

    if (_chdir(path)) {
        printf("Unable to locate the directory: %s\n", path);
        return(-1);
    } else
        system("dir *.wri");
    return(0);
}

int tosomedir(const char* path)
{
    if (_chdir(path)) {
        printf("Unable to locate the directory: %s\n", path);
        return(-1);
    } else
        system("dir *.wri");
    return(0);
}

int tosiblingdir(const char* dirname)
{
    toparentdir();
    tochilddir(dirname);
    return(0);
}

int str2Int(char* str)
{
    int value;
    int i, j; //j is a value of a digit;

    for (value = 0, i = 0; (str[i] != '\0'); i++) {
        j = (int)(str[i] - '0');
        if (j >= 0 && j <= 9) {
            value = value * 10 + j;
        } else {
            return(-1);
        }
    }
    return(value);
}
int createdir(char* path) /*For windows*/
{
    char systemcomand[200];
    sprintf(systemcomand, "mkdir %s", path);
    system(systemcomand);
    /*try
    {
       // Determine whether the directory exists.
        if (  !Directory::Exists( path ) )
       {
          // Create the directory it does not exist.
     	  Directory::CreateDirectory( path );
       }
    }
    catch ( Exception^ e )
    {
       Console::WriteLine( "The process failed: {0}", e );
    }*/
    return(0);
}
int deletedir(const char* path)
{
    char systemcomand[200];
    sprintf(systemcomand, "rmdir %s", path);
    system(systemcomand);
    /*
    if ( Directory::Exists( target ) )
    {

         // Delete the target to ensure it is not there.
         Directory::Delete( target, true );
    }*/
    return(0);
}
int deletefile(const char* path)
{
    char systemcomand[200];
    sprintf(systemcomand, "del %s", path);
    system(systemcomand);
    /*
    if ( Directory::Exists( target ) )
    {

         // Delete the target to ensure it is not there.
         Directory::Delete( target, true );
    }*/
    return(0);
}
char* getnamefrompath(char* Path, char* filename)
{
    int i;

    for (i = (int)strlen(Path); i >= 0; i--) {

#ifdef WIN32
        if (Path[i] == '\\')
#else
        if (Path[i] == '/')
#endif
        {
            break;
        }
    }
    strcpy(filename, &(Path[i+1]));
    for (i = (int)strlen(filename); i > 0; i--) {
        if (filename[i] == '.') {
            filename[i] = '\0';
        }
    }
    return(filename);
}

